package wiki.zimo.servlet.backstage;

import wiki.zimo.constant.Constant;
import wiki.zimo.dao.CategoryDao;
import wiki.zimo.domain.Category;
import wiki.zimo.servlet.BaseServlet;
import wiki.zimo.utils.JsonUtils;
import wiki.zimo.utils.UUIDUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @version v1.0
 * @auther: 子墨
 * @datetime: 2018/11/11 12:09
 * @description: 处理后台商品分类管理的Servlet
 */
@WebServlet(value = "/categoryManage")
public class CategoryManageServlet extends BaseServlet {

    CategoryDao categoryDao = new CategoryDao();

    /**
     * @auther: 子墨
     * @date: 2018/11/11 12:12
     * @describe: 商品分类列表页
     * @param: []
     * @return: java.lang.String
     * @version v1.0
     */
    public String categoryListPage(HttpServletRequest request, HttpServletResponse response) {
        return Constant.URL_BACKSTAGE_CATEGORY_LIST;
    }

    /**
     * @auther: 子墨
     * @date: 2018/11/11 14:11
     * @describe: 修改或新增商品分类页
     * @param: [request, response]
     * @return: java.lang.String
     * @version v1.0
     */
    public String editCategoryPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String cid = request.getParameter("cid");
        if (cid != null) {
            Category category = categoryDao.findOneById(cid);
            request.setAttribute(Constant.CATEGORY, category);
        }
        return Constant.URL_BACKSTAGE_CATEGORY_ADD;
    }


    /**
     * @auther: 子墨
     * @date: 2018/11/11 13:46
     * @describe: 获取商品分类列表信息接口
     * @param: []
     * @return: java.lang.String
     * @version v1.0
     */
    public String getAllCategory(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<Category> categorys = categoryDao.findAll();
        String json = JsonUtils.listToJson(categorys);
        response.getWriter().print(json);
        return null;
    }

    /**
     * @auther: 子墨
     * @date: 2018/11/11 14:16
     * @describe: 新增或修改商品分类信息接口
     * @param: [request, response]
     * @return: java.lang.String
     * @version v1.0
     */
    public String updateCategory(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String cid = request.getParameter("cid");
        String cname = request.getParameter("cname");
        System.out.println(cid);
        if (cid != null && !cid.equals("")) {
            Category category = categoryDao.findOneById(cid);
            category.setCname(cname);
            categoryDao.update(category);
            request.setAttribute(Constant.MSG, "修改商品分类“" + category.getCname() + "”成功");
        } else {
            Category category = new Category();
            category.setCid(UUIDUtils.getRandomId());
            category.setCname(cname);
            categoryDao.insert(category);
            request.setAttribute(Constant.MSG, "新增商品分类“" + category.getCname() + "”成功");
        }
        return categoryListPage(request, response);
    }

    /**
     * @auther: 子墨
     * @date: 2018/11/11 14:18
     * @describe: 删除商品分类信息接口
     * @param: [request, response]
     * @return: java.lang.String
     * @version v1.0
     */
    public String deleteCategory(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String cid = request.getParameter("cid");
        Category category = categoryDao.findOneById(cid);
        if (category != null) {
            categoryDao.delete(category);
            request.setAttribute(Constant.MSG, "删除商品分类“" + category.getCname() + "”成功");
        }
        return categoryListPage(request, response);
    }

}
